/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.LisSystemRes;
import com.cxhd.lis.model.LisSystemRoleRes;
import com.cxhd.lis.model.RoleModel;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/role")
public class RoleCtrl extends Controller {
    
    private LisSystemRes lisAuth = new LisSystemRes();
    private RoleModel role = new RoleModel();
    private LisSystemRoleRes roleRes = new LisSystemRoleRes();
    
    public void index() {
        render("/view/role.jsp");
    }
    
    public void getList() {
        
        String sql = "select * ";
        String serarchContent = getPara("content", null);
        String param = "from lis_system_role order by createtime desc";
        if (serarchContent != null) {
            param = "from study a LEFT JOIN patient b on a.patient_fk=b.pk where b.pat_name like '%" + serarchContent + "%' or b.pat_id like '%" + serarchContent + "%' order by a.created_time desc";
            
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", 1), getParaToInt("pageSize", 10), sql, param);
        
        renderJson(userPage);
    }
    
    public void getAuth() {
        renderJson(lisAuth.getData());
    }
    
    @Before(Tx.class)
    public void saveRole() {
        int pk = role.saveRole(this);
        
        String auth = getPara("auth");
        if (auth != null) {
            roleRes.deleteUserRole(pk + "");
            String auths[] = auth.split(",");
            for (String auth_pk : auths) {
                new LisSystemRoleRes().saveData(pk, auth_pk);
            }
        }
        renderText("true");
    }

    public void getRoleAuth() {
        renderJson(roleRes.getRoleAuth(getPara("pk")));
    }

    public void deleteRole() {
        role.deleteById(getPara("pk"));
        Db.update("delete from lis_system_role_res where role_id=" + getPara("pk"));
        renderText("true");
    }
}
